package com.smec.remes.module.query.packing.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.smec.moblie.BR;
import com.smec.moblie.R;
import com.smec.moblie.databinding.LayoutRecyclerviewPackingBinding;
import com.smec.remes.base.SmecBaseRecyclerViewHolder;
import com.smec.remes.bean.query.PackingList;
import com.smec.remes.util.CommonUtils;
import com.smec.remes.util.CopyText;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/1/8.
 */

public class SmecPackingAdapter extends RecyclerView.Adapter<SmecBaseRecyclerViewHolder>  {

    private Context mcontext;
    private List<PackingList> arrayList=new ArrayList<>();
    private List<PackingList> arrayListAll=new ArrayList<>();

    public SmecPackingAdapter(Context context) {
        this.mcontext=context;
    }

    public void setArrayList(List<PackingList> list){
        arrayList=list;
        arrayListAll=list;
        notifyDataSetChanged();
    }

    public  List<PackingList> getList(){
        return arrayListAll;
    }

    public void _compare(PackingList packingList){
        List<PackingList> aa=new ArrayList<>();

        for(int i=0;i<arrayListAll.size();i++){
            if(_get(arrayListAll.get(i).getPackingNo().toUpperCase(),packingList.getPackingNo())
                    &&_get(arrayListAll.get(i).getTitle().toUpperCase(),packingList.getTitle())
                    &&_get(arrayListAll.get(i).getClassName().toUpperCase(),packingList.getClassName())
                    &&_get(arrayListAll.get(i).getIdnrk().toUpperCase(),packingList.getIdnrk())
                    &&_get(arrayListAll.get(i).getGwgno().toUpperCase(),packingList.getGwgno())){
                aa.add(arrayListAll.get(i));
            }
        }
        arrayList=aa;
        notifyDataSetChanged();
    }

    public boolean _get(String a,String b){
        boolean ss=true;
        if(TextUtils.isEmpty(a)&&TextUtils.isEmpty(b)){
            ss=true;
        }else {
            if(!a.contains(b)){
                ss=false;
            }
        }

        return ss;
    }

    @Override
    public SmecBaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutRecyclerviewPackingBinding layoutRecyclerviewPackingBinding=DataBindingUtil.inflate
                    (LayoutInflater.from(parent.getContext()), R.layout.layout_recyclerview_packing,parent,false);
            SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                    SmecBaseRecyclerViewHolder(layoutRecyclerviewPackingBinding.getRoot());
            smecBaseRecyclerViewHolder.setViewType(viewType);
            smecBaseRecyclerViewHolder.setBinding(layoutRecyclerviewPackingBinding);
            return smecBaseRecyclerViewHolder;
    }

    @Override
    public void onBindViewHolder(SmecBaseRecyclerViewHolder holder, final int position) {
        LayoutRecyclerviewPackingBinding layoutRecyclerviewPackingBinding= (LayoutRecyclerviewPackingBinding) holder.getBinding();
        layoutRecyclerviewPackingBinding.setVariable(BR.packlistnew,arrayList.get(position));
        layoutRecyclerviewPackingBinding.llCopy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CopyText._copy(arrayList.get(position).getIdnrk(),mcontext);
                Toast.makeText(mcontext,"复制成功",Toast.LENGTH_SHORT).show();
            }
        });
        holder.getBinding().executePendingBindings();
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

}
